package com.wxtwxt.ruralmall.ware.service.impl;

import com.wxtwxt.ruralmall.common.to.StockTo;
import com.wxtwxt.ruralmall.common.utils.R;
import com.wxtwxt.ruralmall.ware.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxtwxt.ruralmall.common.utils.PageUtils;
import com.wxtwxt.ruralmall.common.utils.Query;

import com.wxtwxt.ruralmall.ware.dao.WareSkuDao;
import com.wxtwxt.ruralmall.ware.entity.WareSkuEntity;
import com.wxtwxt.ruralmall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if(StringUtils.isNotEmpty(skuId)) {
            queryWrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if(StringUtils.isNotEmpty(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addOrUpdateStock(Long skuId, Long wareId, Integer skuNum) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", skuId).eq("ware_id", wareId);
        WareSkuEntity skuEntity = this.getOne(queryWrapper);
        if(skuEntity != null) {
            baseMapper.updateStock(skuId, wareId, skuNum);
        }else {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            R r = productFeignService.getSkuNameBySkuName(skuId);
            if(r.getCode() != 0) {
                log.error("远程服务调用失败");
            }
            wareSkuEntity.setSkuName((String) r.get("skuName"));
            this.save(wareSkuEntity);
        }
    }

    @Override
    public List<StockTo> stockIsExist(List<Long> skuIds) {
        if(skuIds.size() > 0) {
            return skuIds.stream().map(sku -> {
                StockTo stockTo = new StockTo();
                stockTo.setSkuId(sku);
                Long stock = baseMapper.getStockBySkuId(sku);
                stockTo.setHasStock(stock != null && stock > 0);
                return stockTo;
            }).collect(Collectors.toList());
        }
        return null;
    }

}